package File流;

import java.io.*;

/**
 * @author chihb
 * @create-time 2024/7/5 20:49
 * @description
 */
public class Tes3 {
    public static void main(String[] args) throws Exception {
        try (
                BufferedReader br=new BufferedReader(new FileReader("D:/chiab/questions.txt"));
             BufferedWriter bw=new BufferedWriter(new FileWriter("E:/javaSE/result.txt"));
             ){
            String line;
            while ((line=br.readLine())!=null){
                System.out.println(calculate(line));
                bw.write(line+"="+calculate(line)+'\n');
            }
        }
    }
    private static String calculate(String s) {
        String[] split = s.split("\\s+");
        double a, b;
        for (int i = 0; i < split[0].length(); i++) {
            if (split[0].charAt(i) == '+') {
                a = Double.parseDouble(split[0].substring(0, i));
                b = Double.parseDouble(split[0].substring(i + 1));
                return String.valueOf(a + b);
            } else if (split[0].charAt(i) == '-') {
                a = Double.parseDouble(split[0].substring(0, i));
                b = Double.parseDouble(split[0].substring(i + 1));
                return String.valueOf(a - b);
            } else if (split[0].charAt(i) == '*') {
                a = Double.parseDouble(split[0].substring(0, i));
                b = Double.parseDouble(split[0].substring(i + 1));
                return String.valueOf(a * b);
            } else if (split[0].charAt(i) == '/'){
                a = Double.parseDouble(split[0].substring(0, i));
                b = Double.parseDouble(split[0].substring(i + 1));
                if (b == 0.0) {
                    return "Division by zero";
                }
                return String.valueOf(a / b);
            }
        }
        return null;
    }
}
